﻿<?

$USER_ONLINE = $_POST["USER_ONLINE"];
session_start();

$session = session_id();
$time = time();
$time_check = $time - 30; //SET TIME 10 Minute

include ("Connect_To_Database.php");

$tbl_name10 = "user_online"; // Table name

if ($USER_ONLINE == "StartWebPage") {

    $sql = "SELECT * FROM $tbl_name10 WHERE session='$session'";
    $result = mysql_query($sql);

    $count = mysql_num_rows($result);

    if ($count == "0") {
        $sql1 = "INSERT INTO $tbl_name10(session, time)VALUES('$session', '$time')";
        $result1 = mysql_query($sql1);
    } else {
        "$sql2=UPDATE $tbl_name10 SET time='$time' WHERE session = '$session'";
        $result2 = mysql_query($sql2);
    }

    $sql3 = "SELECT * FROM $tbl_name10";
    $result3 = mysql_query($sql3);

    $count_user_online = mysql_num_rows($result3);

    echo "ขณะนี้มีผู้ใช้งานอยู่: $count_user_online  คน" ;


    $sql4 = "DELETE FROM $tbl_name10 WHERE time<$time_check";
    $result4 = mysql_query($sql4);
}
mysql_close();
?>